﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class Schedule_OneNoteSet : System.Web.UI.Page
{
    NoteManagerBLL nb = new NoteManagerBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                this.BindDateNote();
            }
            else
            {
                User userNo = (User)Session["userManage"];
                User user = (User)userNo;
                lblUser.Text = user.UserId ;
            }
        }
    } 

    /// <summary>
    /// 添加便签
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveNote_Click(object sender, EventArgs e)
    {
        Note note = new Note();
        note.NoteTitle = txtNoteTitle.Text.Trim();//标题
        note.NoteContent = txtNoteContent.Text.Trim();//便签内容
        note.NoteCreateTime = DateTime.Now;//创建时间
        if (Session["userManage"] != null)
        {
            List<User> userNo = (List<User>)Session["userManage"];
            User user= (User)userNo[0];
            note.User.UserId = user.UserId;
          
        }

        bool result;
        if (Request.QueryString["id"] != null)
        {
            note.NoteNo = Convert.ToInt32(Request.QueryString["id"]);
            result = nb.UpdateNote(note);
        }
        else
        {
            result = nb.AddNote(note);//添加标签
        }
        if (result == true)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "key", "<script>alert('标签保存成功！')</script>");
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "key", "<script>alert('标签保存失败！')</script>");
        }
    }

    /// <summary>
    /// 修改便签
    /// </summary>
    private void BindDateNote()
    {
        
            List<Note> note=nb.GetNote(Convert.ToInt32(Request.QueryString["id"]));//根据便签编号查找便签信息
            foreach (var item in note)
            {
                txtNoteTitle.Text=item.NoteTitle;
                txtNoteContent.Text = item.NoteContent;//修改便签
                lblUser.Text = new UserManager().ShowUserInfoByUserNo(item.User.UserNo).UserId;
                lblTime.Text = item.NoteCreateTime.ToString();
            }        
    }

    /// <summary>
    /// 删除便签
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDeleteNote_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            int noteNo = Convert.ToInt32(Request.QueryString["id"]);//获取便签编号
           bool result= nb.DeleteNote(noteNo);    //删除指定便签
           if (result == true)
           {
               Response.Write("<script>alert('删除成功')</script>");
           }
           else
           {
               Response.Write("<script>alert('删除过程中发生异常错误！');</script>");
           }
        }
    }
}